//
//  ActiviteApplyVC.m
//  KuWoCan
//
//  Created by hwd on 17/3/15.
//  Copyright © 2017年 hwd. All rights reserved.
//

#import "ActiviteApplyVC.h"
#import "LabTextFieldCell.h"
#import "ApplyAcGoalCell.h"
#import "CargoCell.h"
#import "orderTotalCell.h"
#import "LabTxtViewCell.h"
#import "CargoChooseVC.h"
#import "NWSaleApplyRequest.h"
#import "PhotoManageCell.h"

#define kSaleApplyParaDicKey_name @"name"      //促销活动名称
#define kSaleApplyParaDicKey_objective @"objective"    //促销目的（逗号分隔）
#define kSaleApplyParaDicKey_target @"target"  //促销目标
#define kSaleApplyParaDicKey_executionTime @"executionTime"   //执行时间
#define kSaleApplyParaDicKey_city @"city"    //city
#define kSaleApplyParaDicKey_ranges @"ranges"   //范围
#define kSaleApplyParaDicKey_content @"content"  //促销活动内容和形式
#define kSaleApplyParaDicKey_materiel @"materiel"  //使用广促物料数量及费用
#define kSaleApplyParaDicKey_totalExpenses @"totalExpenses"  //费用总计
#define kSaleApplyParaDicKey_salesVolume @"salesVolume"  //销售额
#define kSaleApplyParaDicKey_ratio @"ratio"   //费效比率
#define kSaleApplyParaDicKey_activitySales @"activitySales" //期间销售额
#define kSaleApplyParaDicKey_activityRatio @"activityRatio"  //期间费效比率
#define kSaleApplyParaDicKey_promotionAProductList   @"promotionAProductList"   //促销产品（产品明细）
#define kSaleApplyParaDicKey_promotionEProductList   @"promotionEProductList"   //核销产品

//#define kSaleApplyParaDicKey_totalCost @"totalCost"  //总费用
//#define kSaleApplyParaDicKey_supportDepartment @"supportDepartment"   //支持/配合部门
//#define kSaleApplyParaDicKey_agency @"agency"   //经销商
//#define kSaleApplyParaDicKey_proposer @"proposer"   //申请人
//#define kSaleApplyParaDicKey_superior @"superior"    //上级



@interface ActiviteApplyVC () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) TPKeyboardAvoidingTableView *tabView;

@property (nonatomic,strong) NSMutableDictionary *paraDic;

@property (nonatomic,strong) NSMutableArray *paraDataArr;

@property (nonatomic,copy) NSArray *keysArr;

@property (nonatomic,copy) NSArray *leftStrArr;

@property(nonatomic,assign)NSInteger totalBox;

@property(nonatomic,assign)CGFloat totalPrice;

@property (nonatomic,assign) BOOL isNotDetailModel_Cargo;

@end

@implementation ActiviteApplyVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    if (self.moduleType == HomeModuleTypeSA) {
        self.title = @"促销活动";
    } else {
        self.title = @"促销活动核销";
    }
    
    _leftStrArr = @[
                    @[
                        @"促销活动名称",
                        @"促销产品"
                        ],
                    @[
                        @"活动目的"
                        ],
                    @[
                        @"执行城市"
                        ],
                    @[
                        @"执行范围",
                        @"执行时间"
                        ],
                    @[
                        @"内容及形式",
                        @"需要物料",
                        @"费用总计",
                        @"销售额",
                        @"费效比率",
                        @"期间销售额",
                        @"期间费效比率"
                        ]
                    ];
    _keysArr = @[
                 @[
                     kSaleApplyParaDicKey_name,
                     kSaleApplyParaDicKey_promotionAProductList
                     ],
                 @[
                     kSaleApplyParaDicKey_objective
                     ],
                 @[
                     kSaleApplyParaDicKey_city
                     ],
                 @[
                     kSaleApplyParaDicKey_ranges,
                     kSaleApplyParaDicKey_executionTime
                     ],
                 @[
                     kSaleApplyParaDicKey_content,
                     kSaleApplyParaDicKey_materiel,
                     kSaleApplyParaDicKey_totalExpenses,
                     kSaleApplyParaDicKey_salesVolume,
                     kSaleApplyParaDicKey_ratio,
                     kSaleApplyParaDicKey_activitySales,
                     kSaleApplyParaDicKey_activityRatio
                     ]
                 ];
    
    [self objectParaArr];
    [self paraDataArr];
    [self paraDic];
    [self cargoListArr];
    
    if (_isChangeInfo)
    {
        [self prepareDataForChangeOrder];
    }
    
    [self setUpUI];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    KwSegViewHelper *kwHelper = [[KwSegViewHelper alloc] init];
    kwHelper.kwSegIndex = 0;
}

- (void)prepareDataForChangeOrder  //修改订单进入后 显示传来的数据
{
    [_paraDic setValue:_detailInfoModel.name  forKey:kSaleApplyParaDicKey_name];
    [_paraDic setValue:_detailInfoModel.objective forKey:kSaleApplyParaDicKey_objective];
    [_paraDic setValue:_detailInfoModel.target forKey:kSaleApplyParaDicKey_target];
    [_paraDic setValue:_detailInfoModel.city forKey:kSaleApplyParaDicKey_city];
    [_paraDic setValue:_detailInfoModel.ranges forKey:kSaleApplyParaDicKey_ranges];
    [_paraDic setValue:_detailInfoModel.executionTime forKey:kSaleApplyParaDicKey_executionTime];
    [_paraDic setValue:_detailInfoModel.content forKey:kSaleApplyParaDicKey_content];
    [_paraDic setValue:_detailInfoModel.materiel forKey:kSaleApplyParaDicKey_materiel];
    [_paraDic setValue:_detailInfoModel.ratio forKey:kSaleApplyParaDicKey_ratio];
    [_paraDic setValue:_detailInfoModel.salesVolume forKey:kSaleApplyParaDicKey_salesVolume];
    [_paraDic setValue:_detailInfoModel.activitySales forKey:kSaleApplyParaDicKey_activitySales];
    [_paraDic setValue:_detailInfoModel.activityRatio forKey:kSaleApplyParaDicKey_activityRatio];
    
    if (_cargoListArr && _cargoListArr.count > 0)
    {
        for (int i = 0; i<_cargoListArr.count; i++)
        {
            SADetailCargoModel *model = _cargoListArr[i];
            [_paraDataArr addObject:@{kOrderItemPara_Key_ProductAttributeId:model.productAttributeId,kOrderItemPara_Key_ProductName:[NSString stringWithFormat:@"%@,%@,%@",model.kwProductAttribute.product.productName?:@"",model.kwProductAttribute.tasteStr?:@"",model.kwProductAttribute.specificationsStr?:@""],kOrderItemPara_Key_Price:model.kwProductAttribute.price?:@"",kOrderItemPara_Key_Quantity:model.quantity?:@""}];
        }
        NSString *dataStr = [CommonTool beJsonStrWithObj:_paraDataArr];
        if (_moduleType == HomeModuleTypeSC)
        {
            [_paraDic setValue:dataStr forKey:kSaleApplyParaDicKey_promotionEProductList];
        }
        else
        {
            [_paraDic setValue:dataStr forKey:kSaleApplyParaDicKey_promotionAProductList];
        }
        
    }
    //合计
    NSInteger aTotalBox = 0;
    CGFloat aTotalPrice = 0;
    for (int i = 0; i<_cargoListArr.count; i++)
    {
        SADetailCargoModel *model = _cargoListArr[i];
        aTotalBox = aTotalBox + [model.quantity integerValue];
        aTotalPrice = aTotalPrice + [model.kwProductAttribute.price floatValue]*[model.quantity integerValue];
    }
    _totalBox = aTotalBox;
    _totalPrice = aTotalPrice;
    
    _objectParaArr = [NSMutableArray arrayWithArray:[_detailInfoModel.objective componentsSeparatedByString:@","]];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSMutableArray *)objectParaArr
{
    if (!_objectParaArr)
    {
        _objectParaArr = [NSMutableArray array];
    }
    return _objectParaArr;
}

- (NSMutableArray *)paraDataArr
{
    if (!_paraDataArr)
    {
        _paraDataArr = [NSMutableArray array];
    }
    return _paraDataArr;
}

- (NSMutableArray *)cargoListArr
{
    if (!_cargoListArr)
    {
        _cargoListArr = [NSMutableArray array];
    }
    return _cargoListArr;
}

- (NSMutableDictionary *)paraDic
{
    if (!_paraDic)
    {
        _paraDic = [NSMutableDictionary dictionary];
    }
    return _paraDic;
}

- (void)setUpUI
{

    //创建列表
    _tabView = [[TPKeyboardAvoidingTableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStyleGrouped];
    _tabView.backgroundColor = KwBgColor;
    _tabView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tabView.estimatedRowHeight = 40.0;
    _tabView.rowHeight = UITableViewAutomaticDimension;
    _tabView.delegate = self;
    _tabView.dataSource = self;
    [self.view addSubview:_tabView];
    [_tabView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@0);
        make.left.right.equalTo(@0);
        make.bottom.equalTo(@-55);
    }];
    
    //创建底部按钮
    UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [confirmBtn setBackgroundColor:KwBlueColor];
    [confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [confirmBtn setTitle:@"提交" forState:UIControlStateNormal];
    confirmBtn.titleLabel.font = KwFont(16.0);
    [confirmBtn addTarget:self action:@selector(startPlaceAnOrder:) forControlEvents:UIControlEventTouchUpInside];
    confirmBtn.layer.masksToBounds = YES;
    confirmBtn.layer.cornerRadius = 5.0;
    [self.view addSubview:confirmBtn];
    [confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(@-5);
        make.top.equalTo(_tabView.mas_bottom).offset(1);
        make.left.equalTo(@5);
        make.right.equalTo(@-5);
    }];
    
}

//提交申请
- (void)startPlaceAnOrder:(UIButton *)Btn
{
    NSLog(@"%@",_paraDic);
    [self.view endEditing:YES];
    Btn.enabled = NO;
   
    //去除空自空字符串字典
//    for (NSString *key in [_paraDic allKeys]) {
//        if ([_paraDic[key] isEqual:@""] || [_paraDic[key] isEqual:[NSNull class]]) {
//            [_paraDic removeObjectForKey:key];
//        }
//    }
    if (![ToolViewViewController isNotNullStr:_paraDic[kSaleApplyParaDicKey_name]]) {
        [SVProgressHUD showErrorWithStatus:@"请添加促销活动名称"];
        Btn.enabled = YES;
        return;
    }
    if (![ToolViewViewController isNotNullStr:_paraDic[kSaleApplyParaDicKey_promotionAProductList]]) {
        [SVProgressHUD showErrorWithStatus:@"请添加促销产品"];
        Btn.enabled = YES;
        return;
    }
    if (![ToolViewViewController isNotNullStr:_paraDic[kSaleApplyParaDicKey_objective]]) {
        [SVProgressHUD showErrorWithStatus:@"请添加活动目的"];
        Btn.enabled = YES;
        return;
    }
    if (![ToolViewViewController isNotNullStr:_paraDic[kSaleApplyParaDicKey_city]]) {
        [SVProgressHUD showErrorWithStatus:@"请添加执行城市"];
        Btn.enabled = YES;
        return;
    }
    if (![ToolViewViewController isNotNullStr:_paraDic[kSaleApplyParaDicKey_ranges]]) {
        [SVProgressHUD showErrorWithStatus:@"请添加执行范围"];
        Btn.enabled = YES;
        return;
    }
    if (![ToolViewViewController isNotNullStr:_paraDic[kSaleApplyParaDicKey_executionTime]]) {
        [SVProgressHUD showErrorWithStatus:@"请添加执行时间"];
        Btn.enabled = YES;
        return;
    }
    if (![ToolViewViewController isNotNullStr:_paraDic[kSaleApplyParaDicKey_content]]) {
        [SVProgressHUD showErrorWithStatus:@"请添加内容及形式"];
        Btn.enabled = YES;
        return;
    }
    if (![ToolViewViewController isNotNullStr:_paraDic[kSaleApplyParaDicKey_materiel]]) {
        [SVProgressHUD showErrorWithStatus:@"请添加需要物料"];
        Btn.enabled = YES;
        return;
    }
    if (![ToolViewViewController isNotNullStr:_paraDic[kSaleApplyParaDicKey_totalExpenses]]) {
        [SVProgressHUD showErrorWithStatus:@"请添加费用总计"];
        Btn.enabled = YES;
        return;
    }
    if (![ToolViewViewController isNotNullStr:_paraDic[kSaleApplyParaDicKey_salesVolume]]) {
        [SVProgressHUD showErrorWithStatus:@"请添加销售额"];
        Btn.enabled = YES;
        return;
    }
    if (![ToolViewViewController isNotNullStr:_paraDic[kSaleApplyParaDicKey_ratio]]) {
        [SVProgressHUD showErrorWithStatus:@"请添加费效比率"];
        Btn.enabled = YES;
        return;
    }
    if (![ToolViewViewController isNotNullStr:_paraDic[kSaleApplyParaDicKey_activitySales]]) {
        [SVProgressHUD showErrorWithStatus:@"请添加期间销售额"];
        Btn.enabled = YES;
        return;
    }
    if (![ToolViewViewController isNotNullStr:_paraDic[kSaleApplyParaDicKey_activityRatio]]) {
        [SVProgressHUD showErrorWithStatus:@"请添加期间费效比率"];
        Btn.enabled = YES;
        return;
    }

    NSInteger number =  12;
    //商城
    if (_moduleType == HomeModuleTypeSA) {
        if (!_paraDic[@"promotionAProductList"]) {
            number--;
        }
    } else {
        if (!_paraDic[@"promotionEProductList"]) {
            number--;
        }
    }
 
    
    if ([_paraDic allKeys].count >= number)
    {
        if (!_isChangeInfo)
        {
            [_paraDic setValue:@"1" forKey:@"approvalStatus"];
            [_paraDic setValue:@"0" forKey:@"approvalType"];
            [_paraDic setValue:[Singleton shareInstance].userId forKey:@"user.id"];
            if (_moduleType == HomeModuleTypeSC) {
                [_paraDic setValue:_detailInfoModel.approvalStatus forKey:@"approvalStatus"];
                [_paraDic setValue:_detailInfoModel.approvalType forKey:@"approvalType"];
            }
        }
        else
        {
            [_paraDic setValue:[Singleton shareInstance].userId forKey:@"user.id"];
            [_paraDic setValue:_orderId forKey:@"id"];
        }
    }
    else
    {
        [SVProgressHUD showErrorWithStatus:@"请完善信息"];
        Btn.enabled = YES;
        return;
    }
    
    NWSaleApplyRequest *request = [[NWSaleApplyRequest alloc] init];
    [request completion:^(id result, NSString *message, id extension, BOOL succ) {
        if (succ)
        {
            [SVProgressHUD showSuccessWithStatus:@"提交成功"];
            Btn.enabled = YES;
            if (self.picHandle) {
                self.picHandle(self.sitePhotoArr, self.paperPhotoArr);
            }
            if (_moduleType == HomeModuleTypeSA) {
                [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
            } else {
                if (self.reloadBlock) {
                    self.reloadBlock();
                }
                [self.navigationController popViewControllerAnimated:YES];
            }
        } else {
            Btn.enabled = YES;
            [SVProgressHUD showErrorWithStatus:message?:@"提交失败"];
        }
    }];
    
    if (!_isChangeInfo)
    {
        [request startRequestCommitSaleApplyWithDic:_paraDic];
         Btn.enabled = YES;
    }
    else
    {
        if (_moduleType == HomeModuleTypeSC)
        {
            [_paraDic removeObjectsForKeys:@[@"approvalStatus", @"approvalType"]];
            [_paraDic setObject:[Singleton shareInstance].userId forKey:@"user.id"];
            [request startRequestUpdateSaleCheckWithDic:_paraDic];
        }
        else
        {
            [request startRequestUpdateSaleApplyWithDic:_paraDic];
        }
        Btn.enabled = YES;
    }
}

- (void)onTapLeftBarButton {
    if (self.picHandle) {
        self.picHandle(self.sitePhotoArr, self.paperPhotoArr);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark tableViewDelegate,DataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (_isShowHXBZ) {
        return 8;
    }
    return 7;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
   if(_isShowHXBZ && section == 7)
    {
        UIView *headView = [self createHeaderViewWithTitle:@"费用核销标准"];
        return headView;
    }
    else
    {
        return nil;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0)
    {
        if (_cargoListArr.count > 0)
        {
            return 3+_cargoListArr.count;
        }
        else
        {
            return 3;
        }
    }
    else if(section == 5 || section == 6)
    {
        if (_moduleType == HomeModuleTypeSC)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
    if (_isShowHXBZ && section == 7) {
        return 1;
    }
    else
    {
        NSArray *tempArr = _leftStrArr[section];
        return tempArr.count;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if(section == 5 || section == 6)
    {
        if (_moduleType == HomeModuleTypeSC)
        {
            return 10;
        }
        else
        {
            return 0.0001;
        }
    }  if (_isShowHXBZ && section == 7) {
        return 50;
    }
    else
    {
        return 0.0001;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.00001;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *labTextFieldCell_ID = [NSString stringWithFormat:@"LabTextFieldCell%ld%ld",indexPath.section,indexPath.row];
    static NSString *applyAcGoalCell_ID = @"ApplyAcGoalCell";
    static NSString *cargoCell_ID = @"CargoCell";
    static NSString *orderTotalCell_ID = @"orderTotalCell";
    static NSString *labTxtViewCell_ID = @"LabTxtViewCell";
    static NSString *photoManageCell_ID = @"PhotoManageCell";
    
    if (indexPath.section == 1)
    {
        //活动目的
        ApplyAcGoalCell *cell = [tableView dequeueReusableCellWithIdentifier:applyAcGoalCell_ID];
        if (!cell)
        {
            cell = [[ApplyAcGoalCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:applyAcGoalCell_ID];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        for (NSString *numStr in _objectParaArr)
        {
            UIButton *btn = [cell viewWithTag:100+[numStr integerValue]-1];
            btn.selected = YES;
        }
        __weak ApplyAcGoalCell *weakCell = cell;
        cell.block = ^(NSInteger tag){
            UIButton *btn = [weakCell viewWithTag:tag];
            NSString *indexStr;
            if (btn.selected)
            {
                if (_objectParaArr.count > 0)
                {
                    for (NSString *str in _objectParaArr)
                    {
                        if ([str integerValue] == tag-100+1)
                        {
                            indexStr = [NSString stringWithFormat:@"%ld",(long)[_objectParaArr indexOfObject:str]];
                        }
                    }
                    if (!indexStr)
                    {
                        [_objectParaArr addObject:[NSString stringWithFormat:@"%ld",tag-100+1]];
                    }
                }
                else
                {
                    [_objectParaArr addObject:[NSString stringWithFormat:@"%ld",tag-100+1]];
                }

            }
            else
            {
                if (_objectParaArr.count > 0)
                {
                    for (NSString *str in _objectParaArr)
                    {
                        if ([str integerValue] == tag-100+1)
                        {
                            indexStr = [NSString stringWithFormat:@"%ld",(long)[_objectParaArr indexOfObject:str]];
                        }
                    }
                    if (indexStr)
                    {
                        [_objectParaArr removeObject:[NSString stringWithFormat:@"%ld",tag-100+1]];
                    }
                }
            }
            NSString *paraObjsStr = [_objectParaArr componentsJoinedByString:@","];
            NSArray *tempArr = _keysArr[indexPath.section];
            [_paraDic setObject:paraObjsStr forKey:tempArr[indexPath.row]];
        };
        return cell;
    }
    else if (indexPath.section == 0)
    {
        //活动名称
        if (indexPath.row == 0)
        {
            LabTextFieldCell *cell = [tableView dequeueReusableCellWithIdentifier:labTextFieldCell_ID];
            if (!cell)
            {
                cell = [[LabTextFieldCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:labTextFieldCell_ID];
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            NSArray *strArr = _leftStrArr[indexPath.section];
            cell.constStr = strArr[indexPath.row];
            
            NSArray *tempArr = _keysArr[indexPath.section];
            NSString *keyStr = tempArr[indexPath.row];
//            cell.rightTxtF.text = [_paraDic objectForKey:keyStr];
            cell.paraStr =  [_paraDic objectForKey:keyStr];
            cell.block = ^(NSString *str){
                [_paraDic setObject:str forKey:keyStr];
            };
            return cell;
        }
        else
        {
            //酷我商城
            if (_cargoListArr &&_cargoListArr.count>0)
            {
                if (indexPath.row == 1)
                {
                    UITableViewCell *cell;
                    //这一行为 酷我商城
                    cell = [tableView dequeueReusableCellWithIdentifier:@"tempCell_Id"];
                    if (!cell) {
                        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"tempCell_Id"];
                        [self createCellUIWithCell:cell];
                    }
                    cell.selectionStyle = UITableViewCellSelectionStyleNone;
                    return cell;
                }
                else if(indexPath.row == _cargoListArr.count+2)
                {
                    orderTotalCell *cell;
                    //这一行为合计
                    cell = [tableView dequeueReusableCellWithIdentifier:orderTotalCell_ID];
                    if (!cell)
                    {
                        cell = [[orderTotalCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:orderTotalCell_ID];
                    }
                    cell.selectionStyle = UITableViewCellSelectionStyleNone;
                    cell.oTotalParaLab.text = [NSString stringWithFormat:@"¥%.2f",_totalPrice];
                    cell.oNumberLab.text = [NSString stringWithFormat:@"共%ld件商品",(long)_totalBox];
                    return cell;
                }
                else
                {
                    CargoCell *cell;
                    //其余为商品展示
                    cell = [tableView dequeueReusableCellWithIdentifier:cargoCell_ID];
                    if (!cell)
                    {
                        cell = [[CargoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cargoCell_ID];
                    }
                    cell.selectionStyle = UITableViewCellSelectionStyleNone;
                    if (_isChangeInfo)
                    {
                        if (!_isNotDetailModel_Cargo)
                        {
                            if (_cargoListArr && _cargoListArr.count>0)
                            {
                                SADetailCargoModel *model = _cargoListArr[indexPath.row-2];
                                cell.saCargoModel = model;
                            }
                        }
                        else
                        {
                            if (_cargoListArr && _cargoListArr.count>0)
                            {
                                ESSInventoryModel *model = _cargoListArr[indexPath.row-2];
                                cell.model = model;
                            }
                        }
                    }
                    else
                    {
                        if (_cargoListArr && _cargoListArr.count>0)
                        {
                            ESSInventoryModel *model = _cargoListArr[indexPath.row-2];
                            cell.model = model;
                        
                        }
                    }
                    return cell;
                }
            }
            else
            {
                if (indexPath.row == 1)
                {
                    UITableViewCell *cell;
                    //这一行为 酷我商城
                    cell = [tableView dequeueReusableCellWithIdentifier:@"tempCell_Id"];
                    if (!cell) {
                        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"tempCell_Id"];
                        [self createCellUIWithCell:cell];
                    }
                    cell.selectionStyle = UITableViewCellSelectionStyleNone;
                    return cell;
                }
                else
                {
                    UITableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:@"tempCell3"];
                    if (!cell) {
                        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"tempCell3"];
                        UIImageView *img = [UIImageView new];
                        img.image = kImg(@"nodata");
                        [cell addSubview:img];
                        __weak UITableViewCell *weakCell  = cell;
                        [img mas_makeConstraints:^(MASConstraintMaker *make) {
                            make.centerX.equalTo(weakCell.mas_centerX);
                            make.top.equalTo(weakCell.mas_top).offset(30);
                            make.bottom.equalTo(weakCell.mas_bottom).offset(-30);
                            make.size.mas_equalTo(CGSizeMake(150, 120));
                        }];
                    }
                    cell.selectionStyle = UITableViewCellSelectionStyleNone;
                    return cell;
                }
            }
        }
    }
    else if (indexPath.section == 4)
    {
        if (indexPath.row == 1)
        {
            LabTxtViewCell *cell = [tableView dequeueReusableCellWithIdentifier:labTxtViewCell_ID];
            if (!cell)
            {
                cell = [[LabTxtViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:labTxtViewCell_ID];
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.constStr = @"需要物料";
            NSArray *tempArr = _keysArr[indexPath.section];
            NSString *keyStr = tempArr[indexPath.row];
            cell.txtStr = [_paraDic objectForKey:keyStr];
            cell.block = ^(NSString *str){
                [_paraDic setObject:str forKey:keyStr];
            };
            return cell;
        }
        else
        {
            LabTextFieldCell *cell = [tableView dequeueReusableCellWithIdentifier:labTextFieldCell_ID];
            if (!cell)
            {
                cell = [[LabTextFieldCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:labTextFieldCell_ID];
            }
            NSArray *strArr = _leftStrArr[indexPath.section];
            cell.constStr = strArr[indexPath.row];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            NSArray *tempArr = _keysArr[indexPath.section];
            NSString *keyStr = tempArr[indexPath.row];
//            cell.rightTxtF.text = [_paraDic objectForKey:keyStr];
            cell.paraStr =  [_paraDic objectForKey:keyStr];
            cell.block = ^(NSString *str){
                [_paraDic setObject:str forKey:keyStr];
            };
            return cell;
        }
    }
    else if(indexPath.section == 5 || indexPath.section == 6)
    {
        if (_moduleType == HomeModuleTypeSA)
        {
            return nil;
        }
        else
        {
            PhotoManageCell *cell;
            cell = [tableView dequeueReusableCellWithIdentifier:photoManageCell_ID];
            if (!cell)
            {
                cell = [[PhotoManageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:photoManageCell_ID];
            }
            __weak typeof(self) weakSelf = self;
            cell.choosePictureHandle = ^(NSArray<PicListModel *> *picArr) {
                if (indexPath.section == 5) {
                    _sitePhotoArr = picArr.mutableCopy;
                } else {
                    _paperPhotoArr = picArr.mutableCopy;
                }
                [weakSelf.tabView reloadData];
            };
            cell.vc = self;
            cell.isSubmit = _isSubmit;
            if (indexPath.section == 5)
            {
                cell.titleLabel.text = @"活动现场照片管理";
                cell.picType = PicTypeSCSite;
                [cell configCell:_sitePhotoArr];
            }
            else
            {
                cell.titleLabel.text = @"票据照片管理";
                cell.picType = PicTypeSCBill;
                [cell configCell:_paperPhotoArr];
            }
            cell.isChange = _isChangeInfo;
            cell.orderId = _orderId;
            return cell;
        }
    }else if(_isShowHXBZ && indexPath.section == 7)
    {
        TextViewCell *cell;
        cell = [tableView dequeueReusableCellWithIdentifier:@"cellText2"];
        if (!cell)
        {
            cell = [[TextViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellText2"];
        }
        [cell setUpUIWithTextStr:_mbContect];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textView.editable = NO;
        return cell;
    }
    else
    {
        LabTextFieldCell *cell = [tableView dequeueReusableCellWithIdentifier:labTextFieldCell_ID];
        if (!cell)
        {
            cell = [[LabTextFieldCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:labTextFieldCell_ID];
        }
        NSArray *strArr = _leftStrArr[indexPath.section];
        cell.constStr = strArr[indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        NSArray *tempArr = _keysArr[indexPath.section];
        NSString *keyStr = tempArr[indexPath.row];
//        cell.rightTxtF.text = [_paraDic objectForKey:keyStr];
        cell.paraStr =  [_paraDic objectForKey:keyStr];
        cell.block = ^(NSString *str){
            [_paraDic setObject:str forKey:keyStr];
        };
        return cell;
    }
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 && indexPath.row == 1)
    {
//商品改变 字典变
        CargoChooseVC *vc = [[CargoChooseVC alloc] init];
        vc.changeOrderCargos = _cargoListArr;
        vc.isNotDetailModel_Order = _isNotDetailModel_Cargo;
        vc.type = CargoVCTypeOrderManage;
        vc.block = ^(NSMutableArray *cargoArr){
            _cargoListArr = [NSMutableArray arrayWithArray:cargoArr];
            _isNotDetailModel_Cargo = YES;
            NSInteger aTotalBox = 0;
            CGFloat aTotalPrice = 0;
            CGFloat totalWeight = 0;
            for (int i = 0; i<_cargoListArr.count; i++)
            {
                ESSInventoryModel *model = _cargoListArr[i];
                 aTotalBox = aTotalBox + [model.totalBox integerValue];
                aTotalPrice = aTotalPrice + [model.price floatValue]*[model.totalBox integerValue];
                totalWeight = totalWeight + [model.weight integerValue]*[model.totalBox integerValue];
               
            }
            _totalBox = aTotalBox;
            _totalPrice = aTotalPrice;
           
            
            NSIndexSet *indeSec = [NSIndexSet indexSetWithIndex:0];
            [_tabView reloadSections:indeSec withRowAnimation:UITableViewRowAnimationAutomatic];
            
            //加参数
            [_paraDataArr removeAllObjects];
            for (int i = 0; i<cargoArr.count; i++)
            {
                ESSInventoryModel *model = cargoArr[i];
                [_paraDataArr addObject:@{kOrderItemPara_Key_ProductAttributeId:model.productAttributeId,kOrderItemPara_Key_ProductName:[NSString stringWithFormat:@"%@,%@,%@",model.productName,model.tasteName,model.specificationsName],kOrderItemPara_Key_Price:model.price,kOrderItemPara_Key_Quantity:model.totalBox,kOrderItemPara_Key_Weight:model.weight}];
            }
            NSString *dataStr = [CommonTool beJsonStrWithObj:_paraDataArr];
            if (_moduleType == HomeModuleTypeSA)
            {
                [_paraDic setValue:dataStr forKey:kSaleApplyParaDicKey_promotionAProductList];
            }
            else
            {
                [_paraDic setValue:dataStr forKey:kSaleApplyParaDicKey_promotionEProductList];
            }
            
        };
        [self.navigationController pushViewController:vc animated:YES];
    }
}

//创建第一行酷我商城单元格
- (void)createCellUIWithCell:(UITableViewCell *)cell
{
    UIImageView *img = [UIImageView new];
    img.image = kImg(@"shangchen");
    [cell addSubview:img];
    [img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@15);
        make.top.equalTo(@10);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    
    UILabel *lab = [UILabel new];
    lab.text = @"酷我商城";
    lab.font = KwFont(14.0);
    [cell addSubview:lab];
    [lab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(img.mas_right).offset(10);
        make.height.equalTo(@14);
        make.top.equalTo(img.mas_top);
    }];
    
    UIImageView *img2 = [UIImageView new];
    img2.image = kImg(@"next");
    [cell addSubview:img2];
    [img2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@-15);
        make.size.mas_equalTo(CGSizeMake(12, 15));
        make.top.equalTo(img.mas_top);
    }];
    
    UIView *lineView = [UIView new];
    lineView.backgroundColor = KwGaryColor;
    [cell addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(img.mas_bottom).offset(10);
        make.left.right.equalTo(@0);
        make.height.equalTo(@1.5);
        make.bottom.equalTo(@0);
    }];
}
- (UIView *)createHeaderViewWithTitle:(NSString *)title
{
    UIView *headView = [UIView new];
    headView.backgroundColor = [UIColor whiteColor];
    UILabel *lab = [UILabel new];
    lab.text = title;
    lab.textColor = [UIColor blackColor];
    lab.font = KwFont(15);
    [headView addSubview:lab];
    [lab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(headView.mas_left).offset(20);
        make.top.equalTo(headView).offset(15);
        make.height.equalTo(@15);
    }];
    
    UIView *lineView = [UIView new];
    lineView.frame = CGRectMake(0, 50, self.view.bounds.size.width, 1.5);
    lineView.backgroundColor = KwGaryColor;
    [headView addSubview:lineView];
    return headView;
}


@end
